package com.example.utils;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;


import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;


public class ErCodeUtils {



    /**
     * 生成二维码
     * @param content 内容
     * @param
     */
    public static void createQrCodeN(String content, int width, int height, HttpServletResponse response){
        QrConfig config = new QrConfig(width, height);
        //附带logo
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 高纠错级别
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(new Color(0,60,130).getRGB());
// 设置背景色（灰色）
        config.setBackColor(new Color(242,242,242).getRGB());
       /* try {
            OutputStream  out = new FileOutputStream("");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/

        BufferedImage bufferedImage = QrCodeUtil.generate(//
                content, //二维码内容
                config
        );
        try {
            //以JPEG格式向客户端发送
            ServletOutputStream  os = response.getOutputStream();
            ImageIO.write(bufferedImage, "PNG",os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
